package com.tbit.main.service.core.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbit.common.entity.core.UserInfo;
import com.tbit.main.dao.core.UserInfoDao;
import com.tbit.main.pojo.RideLog;
import com.tbit.main.pojo.RideLogPush;
import com.tbit.main.pojo.User;
import com.tbit.main.service.DataService;
import com.tbit.main.service.MachineService;
import com.tbit.main.service.RideLogService;
import com.tbit.main.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author: qiu.longjie
 * @create: 2025-04-30
 */
@Service
@Slf4j
public class UserInfoServiceImpl extends ServiceImpl<UserInfoDao, UserInfo> {

    @Autowired
    private RideLogService rideLogService;

    @Autowired
    private MachineService machineService;

    @Autowired
    private DataService dataService;

    @Autowired
    private UserService userService;

    /**
     * 更新用户最后一次骑行时间。如果当前用户不在从信息表中则新增
     *
     * @param rideLogPush
     */
    public void updateLastRideTime(RideLogPush rideLogPush) {
        // 通过订单编号查询订单信息
        RideLog rideLog = rideLogService.getByOrderNO(rideLogPush.getOrderNO());
        if (rideLog == null){
            log.info("更新用户最后一次骑行时间,订单编号:{} 不存在",rideLogPush.getOrderNO());
            return;
        }
        // 获取用户信息
        UserInfo userInfo = getOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserId, rideLog.getUserId()));
        if (userInfo == null){
            log.info("更新用户最后一次骑行时间,用户编号:{} 不存在。则新增",rideLog.getUserId());
            // 新增用户信息
            userInfo = new UserInfo();
            userInfo.setAccountId(rideLog.getAccountId());
            userInfo.setBrandId(dataService.getParentId(rideLog.getAccountId()));
            userInfo.setUserId(rideLog.getUserId());
            userInfo.setLastRideTime(DateUtil.parse(rideLog.getStartTime(), DatePattern.NORM_DATETIME_PATTERN));
            userInfo.setLastRideAccountId(rideLog.getAccountId());
            User user = userService.getByUserId(rideLog.getUserId());
            if (user != null){
                String joinTime = user.getJoinTime();
                userInfo.setJoinTime(DateUtil.parse(joinTime, DatePattern.NORM_DATETIME_PATTERN));
                userInfo.setJoinDate(userInfo.getJoinTime());
            }
            save(userInfo);
            log.info("更新用户最后一次骑行时间,新增用户信息:{}",userInfo);
        }else {
            userInfo.setLastRideTime(DateUtil.parse(rideLog.getStartTime()));
            userInfo.setLastRideAccountId(rideLog.getAccountId());
            updateById(userInfo);
            log.info("更新用户最后一次骑行时间,更新用户信息:{}",userInfo);
        }

        // 更新设备信息的最后还车时间
        log.info("更新设备信息的最后还车时间,机器编号:{},还车时间:{}",rideLogPush.getMachineNO(),rideLog.getEndTime());
        machineService.updateLastReturnTime(rideLogPush.getMachineNO(), StrUtil.isEmpty(rideLog.getEndTime()) ? new Date() : DateUtil.parse(rideLog.getEndTime(), DatePattern.NORM_DATETIME_PATTERN));
    }
}
